<?php

namespace app\member\controller;

use app\common\controller\Frontend;
use app\admin\model\Member;
use think\Db;

class register extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function index()
    {
        $url = $this->request->request('url', '');
        $this->view->assign('url', $url);
        $this->view->assign('title', __('register'));
        return $this->view->fetch();
    }
    public function regdata(){
        if(session('memberinfo')){
            $this->error("已登录");
        }
        $username = input('username','','trim');
        $password = input('password','','trim');
        $mobile = input('mobile','','trim');
        $smscode= input('smscode','','trim');
        if(empty($username)){
            $this->error('用户名为空');
        }
        if(empty($password)){
            $this->error('密码为空');
        }
        if(empty($mobile)){
            $this->error('手机号为空');
        }
        //正则
        $mobilereg="/^1[3456789]\d{9}$/";
        if(!preg_match($mobilereg,$mobile)){
            $this->error('手机号格式不正确');
        }
        //判断唯一，手机号唯一，用户名唯一
        $isexistmobile=Member::where('mobile',$mobile)->find();
        if($isexistmobile){
            $this->error('手机号已被占用');
        }
        $smscodeinfo = Db::name('smscode')->where('mobile',$mobile)->order('id desc')->find();
        if($smscodeinfo['code']!=$smscode){

            $this->error('验证码错误');
        }
        $memdata=[
            'username'=>$username,
            'password'=>md5($password),
            'mobile'=>$mobile,
        ];
        $res = Member::create($memdata);
        if($res!==false){
            $this->success("注册成功");
        }else{
            $this->error("注册失败");
        }
    }


}
